$(window).on('load', function () {
    $('.selectpicker').selectpicker({
        'selectedText': 'cat'
    });
});
$().ready(function() {
    validateRule();
    selCertificateList();
    $("#expType").val($("#expType1").val());
    $("#province").val($("#province1").val());
    $("#certificateList").val($("#certificateList1").val());
    //省
    $('#adre').citys({province:$("#province1").val()});
    $(".province1").change(function () {
        $("#orgId").empty();
        $("#expType").val("");
        $("#userId").val("");
    });
    //考点选择
    $("#orgId").change(function () {
        $("#expType").val("");
        $("#userId").val("");
        getOrg();
    });
//专家信息下拉
    $("#expType").change(function () {
        $("#userId").empty();
        if($("#expType").val()==''){
            layer.msg("请先选择专家类型");
            return false
        }
        getExpert();
    });
    $("input[name='a']").change(function () {
        getExpert()
    })
});
//考点根据省份下拉
getOrg();
function getOrg(){
    $.ajax({
        url: "/jzweb/personnelArrangement/selProTestCenter",
        data: {province:$("#province1").val()},
        success: function (r) {
            for (var p in r.data) {
                if($("#orgId1").val()==r.data[p].id){
                    $("#orgId").append("<option selected value='" + r.data[p].id + "'>" + r.data[p].name + "</option>");
                }else{
                    $("#orgId").append("<option  value='" + r.data[p].id + "'>" + r.data[p].name + "</option>");
                }

            }
        }
    });
}
//专家
getExpert();
function getExpert(){
    $("#userId").empty();
    $.ajax({
        url: "/jzweb/personnelArrangement/selectExpert",
        data: {
            cbatchId:$("#certificateList1").val(),
            province:$("#province1").val(),
            orgId:$("#orgId1").val(),
            type:$("#expType1").val(),
            status:$('input[name="a"]:checked').val(),
            orgType:$('input[name="orgTypes"]:checked').val(),
        },
        success: function (r) {
            for (var p in r.data) {
                if($("#userId1").val()==r.data[p].id){
                    $("#userId").append("<option selected value='" + r.data[p].id + "'>" + r.data[p].name + "</option>");
                }else{
                    $("#userId").append("<option value='" + r.data[p].id + "'>" + r.data[p].name + "</option>");
                }
            }
            $('.selectpicker').selectpicker('refresh');
        }
    });
}

$.validator.setDefaults({
    submitHandler : function() {
        update();
    }
});
//认证批次下拉框
function selCertificateList() {
    $.ajax({
        url: "/jzweb/personnelArrangement/selCbatchSelect",
        success: function (r) {
            for (var p in r.data) {
                if($("#certificateList1").val()==r.data[p].id){
                    $("#certificateList").append("<option selected value='" + r.data[p].id + "'>" + r.data[p].name + "</option>");
                }else{
                    $("#certificateList").append("<option value='" + r.data[p].id + "'>" + r.data[p].name + "</option>");
                }
            }
        }
    });
}

function update() {
    if($("#userId").val()==''){
        layer.msg("请选择考点人员");
        return false
    }
    $.ajax({
        cache : true,
        type : "POST",
        url : "/jzweb/personnelArrangement/update",
        data : $('#signupForm').serialize(),// 你的formid
        async : false,
        error : function(request) {
            parent.layer.alert("Connection error");
        },
        success : function(data) {
            if (data.code == 0) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);

            } else {
                parent.layer.alert(data.msg)
            }

        }
    });

}
function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules : {
            cbatchId : {
                required : true
            },
            province : {
                required : true
            },
            orgId : {
                required : true
            },
            expType : {
                required : true
            },
            userId : {
                required : true
            }
        },
        messages : {
        }
    })
}